<template>
  <!--弹窗-->
  <el-dialog
    width="30%"
    :title="title"
    :visible.sync="visible"
    :before-close="handleClose">
    <el-form :model="formData" :rules="rules" ref="formData" label-width="100px">
      <el-form-item label="导航标题：" prop="barName">
        <el-input v-model="formData.barName"></el-input>
      </el-form-item>
      <el-form-item label="导航链接:" prop="barUrl">
        <el-input v-model="formData.barUrl"></el-input>
      </el-form-item>
    </el-form>
    <el-row style="margin-top:17px; ">
      <el-button style="float:right;margin-left:6px;" size="mini" type="danger" plain @click="handleClose">取 消</el-button>
      <el-button style="float:right" size="mini" type="primary" @click="submitForm('formData')">确定</el-button>
    </el-row>
  </el-dialog>
</template>
<script>
  // import * as api from '@/api/homepage'
  import qs from "qs"
  export default {
    name: 'Edit',
    data() {
      return {
        opts: {
          navList: []
        },
        ada: {},
        formData: {
          barSort: 1
        },
        rules: {
          barName: [
            { required: true, message: '请选择导航标题', trigger: 'blur' }
          ],
          barUrl: [
            {required: true, message: '请输入导航链接', trigger: 'blur'}
          ]
        }
      }
    },
    props: {
      visible: {
        type: Boolean,
        default: false
      },
      title: {
        type: String,
        default: ''
      }
    },
    mounted() {
      /*this.$store.dispatch('GetOpts', { enumName: "NavEnum", type: 'arr' }).then(res => {
        this.opts.navList = res
      })*/
    },
    methods: {
      handleClose(done) {
        this.formData = {}
        this.$refs['formData'].resetFields()
        this.$emit('close-callback')
      },
      // 获取选中的导航标题
      changeValue(value) {
        let obj = {};
        obj = this.opts.navList.find((item) => {
          return item.code === value;
        });
        if (obj === undefined) {
          this.formData.navTitle = ''
        } else {
          this.formData.navTitle = obj.desc
        }
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (!this.formData.barName) {
            this.$message({
              type: 'error',
              message: '请输入导航标题'
            });
            return false
          }
          if (!this.formData.barUrl) {
            this.$message({
              type: 'error',
              message: '请输入导航链接'
            });
            return false
          }
          if (valid) {
            //新增底部导航栏，给父ID赋值
            this.formData.parentId = 0
            this.loading=true
            this.$axios.post('education-system/sys/head-bar/navBarSave', qs.stringify(this.formData)).then(r=>{
              this.loading=false
              if(r.status == 200 && r.data.obj == true){
                this.$message({
                  type: 'success',
                  message: '操作成功!'
                });
                this.handleClose()
              }else{
                this.$message({
                  type: 'error',
                  message: '提交失败!'
                });
              }
            }).catch(() => {
              this.loading=false
            })
          } else {
            this.$message({
              type: 'error',
              message: "提交失败"
            });
          }
        });
      }
    }
  }
</script>
